using System.Collections;
using System.Collections.Generic;
using UnityEditor.EditorTools;
using UnityEngine;
using UnityEngine.UI;

public class StoreManger : MonoBehaviour
{
    public static StoreManger instance;

    public GameObject prefabItem;

    private GameObject storePanel;

    private GameObjectPoolMgr poolMgr;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }


    void Start()
    {

    }

    //TODO 后边完善
    void BuyItem(int id)
    {
    }

    public void CreateItem(GameObject panel, Store cfg)
    {
        poolMgr = panel.AddComponent<GameObjectPoolMgr>();
        string[] items = cfg.ItemList.Split('#');
        List<int> itemIDs = new List<int>();
        foreach (var item in items)
        {
            if (int.TryParse(item, out int id))
            {
                itemIDs.Add(id);
            }
        }

        poolMgr.InitPool(itemIDs.Count, prefabItem);
        for (int i = 0; i < itemIDs.Count; i++)
        {
            Item itemCfg = DM.Item[itemIDs[i]];
            GameObject item = poolMgr.Spawn();
            item.name = i + "";
            Prefab_StoreItem script = item.GetComponent<Prefab_StoreItem>();
            script.Init(itemCfg);
        }
    }

    public void DestroyItem(GameObject panel)
    {
        poolMgr = panel.GetComponent<GameObjectPoolMgr>();
        poolMgr.Recycle(prefabItem);
    }
}